package nl.stroep.classbuilder.templates 
{
	import flash.utils.ByteArray;
	
	public class Templates 
	{
		private static var _instance:Templates;
				
		[Embed(source='../../../../../lib/ClassTemplate.xml', mimeType="application/octet-stream")]
		private var ClassTemplate:Class;
		private var classTemplateXML:XML;	
		public var classTemplate:String;	

		[Embed(source='../../../../../lib/ObjectTemplate.xml', mimeType="application/octet-stream")]
		private var ObjectTemplate:Class;
		private var objectTemplateXML:XML;	
		public var objectTemplate:String;
		
		[Embed(source='../../../../../lib/ParameterTemplate.xml', mimeType="application/octet-stream")]
		private var ParameterTemplate:Class;
		private var parameterTemplateXML:XML;	
		public var parameterTemplate:String;
		
		
		public function Templates(enforcer:SingletonEnforcer) 
		{
			if (enforcer == null)
			{
				throw new Error("Singleton: Do not instantiate Templates with the constructor.");
			}
		}
		
		public static function getInstance():Templates 
		{
			if(!_instance)
			{
				_instance = new Templates(new SingletonEnforcer());
				
				_instance.init()
			}
			
			return _instance;
		}
		
		private function init():void
		{
			var xmlString:String;
			
			var bytes1:ByteArray = (new ClassTemplate()) as ByteArray;
			xmlString = bytes1.readUTFBytes( bytes1.length );
			classTemplateXML = new XML( xmlString );
			classTemplate = classTemplateXML.template.text();
			
			var bytes2:ByteArray = (new ObjectTemplate()) as ByteArray;
			xmlString = bytes2.readUTFBytes( bytes2.length );
			objectTemplateXML = new XML( xmlString );
			objectTemplate = objectTemplateXML.template.text();
			
			var bytes3:ByteArray = (new ParameterTemplate()) as ByteArray;
			xmlString = bytes3.readUTFBytes( bytes3.length );
			parameterTemplateXML = new XML( xmlString );
			parameterTemplate = parameterTemplateXML.template.text();
		}
	}

}

class SingletonEnforcer { }
